package com.sky.utils;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class DESEncryptor {
	private static byte[] DESIV = { 0x12, 0x34, 0x56, 0x78, (byte) 0x90,
			(byte) 0xAB, (byte) 0xCD, (byte) 0xEF };
	private static final String DES_KEY = "Wr$@com;";// 杩欎釜key鐨勯暱搴﹀繀椤绘槸8鐨勫�嶆暟
	private static final String BM = "UTF-8";

	/**
	 * 鍔犲瘑String 鏄庢枃杈撳叆瀵嗘枃杈撳嚭
	 * 
	 * @param inputString
	 *            寰呭姞瀵嗙殑鏄庢枃
	 * @return 鍔犲瘑鍚庣殑瀛楃涓�
	 */
	public static String getEnc(String inputString) {
		byte[] byteMi = null;
		byte[] byteMing = null;
		String outputString = null;
		try {
			byteMing = inputString.getBytes(BM);
			byteMi = getEncCode(byteMing);
			byte[] temp = Base64.encode(byteMi, Base64.DEFAULT);
			outputString = new String(temp, BM);
		} catch (Exception e) {
		} finally {
			byteMing = null;
			byteMi = null;
		}
		return outputString;
	}

	/**
	 * 瑙ｅ瘑String 浠ュ瘑鏂囪緭鍏ユ槑鏂囪緭鍑�
	 * 
	 * @param inputString
	 *            闇�瑕佽В瀵嗙殑瀛楃涓�
	 * @return 瑙ｅ瘑鍚庣殑瀛楃涓�
	 */
	public static String getDec(String inputString) {
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = null;
		try {
			byteMi = Base64.decode(inputString.getBytes(), Base64.DEFAULT);
			byteMing = getDesCode(byteMi);
			strMing = new String(byteMing, BM);
		} catch (Exception e) {
		} finally {
			byteMing = null;
			byteMi = null;
		}
		return strMing;
	}

	/**
	 * 鍔犲瘑浠yte[]鏄庢枃杈撳叆,byte[]瀵嗘枃杈撳嚭
	 * 
	 * @param bt
	 *            寰呭姞瀵嗙殑瀛楄妭鐮�
	 * @return 鍔犲瘑鍚庣殑瀛楄妭鐮�
	 */
	private static byte[] getEncCode(byte[] bt) {
		byte[] byteFina = null;
		Cipher cipher;
		try {
			DESKeySpec keySpec = new DESKeySpec(DES_KEY.getBytes(BM));// 璁剧疆瀵嗛挜鍙傛暟
			AlgorithmParameterSpec iv = new IvParameterSpec(DESIV);// 璁剧疆鍚戦噺
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 鑾峰緱瀵嗛挜宸ュ巶
			Key key = keyFactory.generateSecret(keySpec);// 寰楀埌瀵嗛挜瀵硅薄
			// 寰楀埌Cipher瀹炰緥
			cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, key, iv);
			byteFina = cipher.doFinal(bt);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	/**
	 * 瑙ｅ瘑浠yte[]瀵嗘枃杈撳叆,浠yte[]鏄庢枃杈撳嚭
	 * 
	 * @param bt
	 *            寰呰В瀵嗙殑瀛楄妭鐮�
	 * @return 瑙ｅ瘑鍚庣殑瀛楄妭鐮�
	 */
	private static byte[] getDesCode(byte[] bt) {
		Cipher cipher;
		byte[] byteFina = null;
		try {
			DESKeySpec keySpec = new DESKeySpec(DES_KEY.getBytes(BM));// 璁剧疆瀵嗛挜鍙傛暟
			AlgorithmParameterSpec iv = new IvParameterSpec(DESIV);// 璁剧疆鍚戦噺
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 鑾峰緱瀵嗛挜宸ュ巶
			Key key = keyFactory.generateSecret(keySpec);// 寰楀埌瀵嗛挜瀵硅薄
			// 寰楀埌Cipher瀹炰緥
			cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, key, iv);
			byteFina = cipher.doFinal(bt);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cipher = null;
		}
		return byteFina;
	}
	
	public static String MD5(String str)
	{
		MessageDigest md5;
		byte[] bytes = null;
		
		try {
			md5 = MessageDigest.getInstance("MD5");
			bytes = md5.digest(str.getBytes("utf-8"));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return new String(bytes);
	}
	
//	public static void main(String args[]) { 
//		
//		String eee = DESEncryptor.getEnc("106904008852522");
//		System.out.println(eee);
//		
//		String dec =  DESEncryptor.getDec("8KVfIPqPR7FxDHQ4rWRLnS+gQwR6b9+igCXjgfMPIH6m2CmdJ/VkFOTIWY4UBO8xRqJrfi3WFf6jJs4/upo6WOYp1NnIL9yJkDzDdbTpiLa5KFISWfz24cnsq00at+n9Ycr4eqiSxVICFzjsi70NUhtEzreNuJjwjwyhN7/wmPx46F0xXKatNR5d7diOg5fPkdPiDSpWRAxPcjsbtClct6nKVeZOd2bRyRYNBcypsOD1rXehPIlEKQ==");
//		System.out.println(dec);
//	}
	
}
